package com.jiaoyou.demo.Controller;

import com.alibaba.fastjson.JSONObject;
import com.jiaoyou.demo.api.HttpClientUtil;
import com.jiaoyou.demo.api.ResultCode;
import com.jiaoyou.demo.api.UserConstantInterface;
import com.jiaoyou.demo.entity.User;
import com.jiaoyou.demo.service.UserService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 用户管理控制器
 * @author Wang
 *
 **/

@RestController
@RequestMapping("/user")
public class logincontroller {

    @Autowired
    private UserService userService;


    @PostMapping("/register")
    public JsonResult register(                 //登录检测
            @RequestParam("code") String code
    ){
        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret", UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult = HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        // 获取参数返回的
        String session_key = jsonObject.get("session_key").toString();
        String open_id = jsonObject.get("openid").toString();
        // 根据返回的user实体类，判断用户是否是新用户
        User user = userService.queryByOpenId(open_id);
        if(user == null){
            return new JsonResult(0,"未注册");//未注册
        }


        // 封装返回小程序
        Map<String, String> result = new HashMap<>();
        result.put("userid", user.getUid().toString());
        result.put("username", user.getUsername());
        result.put("student_id", user.getStudent_id().toString());
        result.put("qqnumber",user.getQq());
        result.put("phone_num",user.getMobile());
        result.put("position",user.getPosition().toString());
        result.put("session_key", session_key);
        result.put("open_id", open_id);
        return new JsonResult(ResultCode.SUCCESS, result);

    }
    @PostMapping("/edit")
    public Object edit(                // 编辑
            @RequestParam("userid")int userid,
            @RequestParam("username")String username,
            @RequestParam("student_id")int student_id,
            @RequestParam("qqnumber")String qq,
            @RequestParam("phone")String phone,
            @RequestParam("position")int position)
    {
        User user=userService.queryById(userid);
        if(user!=null){
            user.setStudent_id(student_id);
            user.setUsername(username);
            user.setQq(qq);
            user.setMobile(phone);
            user.setPosition(position);
            userService.update(user);
            return new JsonResult();
        }
        return new JsonResult(0,"无此用户");
    }
    @PostMapping("/login")
    public JsonResult user_login(       //用户注册
            @RequestParam("code") String code,
            @RequestParam("username") String userName,
            @RequestParam("student_id")int student_id,
            @RequestParam("qqnumber")String qq,
            @RequestParam("phone")String phone,
            @RequestParam("position")int position
    ){
        // 配置请求参数
        Map<String, String> param = new HashMap<>();
        param.put("appid", UserConstantInterface.WX_LOGIN_APPID);
        param.put("secret", UserConstantInterface.WX_LOGIN_SECRET);
        param.put("js_code", code);
        param.put("grant_type", UserConstantInterface.WX_LOGIN_GRANT_TYPE);
        // 发送请求
        String wxResult = HttpClientUtil.doGet(UserConstantInterface.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        // 获取参数返回的
        String session_key = jsonObject.get("session_key").toString();
        String open_id = jsonObject.get("openid").toString();
        // 根据返回的user实体类，判断用户是否是新用户，不是的话，更新，是的话，将用户信息存到数据库
        User user = userService.queryByOpenId(open_id);
        if(user != null){
//            user.setUserNewLogin(new Date());
            userService.update(user);
        }else{                  //写入数据库部分
            User insert_user = new User();
            insert_user.setUsername(userName);
            insert_user.setStudent_id(student_id);
            insert_user.setPosition(position);
            insert_user.setQq(qq);
            insert_user.setMobile(phone);
            insert_user.setOpenid(open_id);
            System.out.println("insert_user:"+insert_user.toString());
            // 添加到数据库
            User flag=userService.insert(insert_user);
            if(!flag.getOpenid().isEmpty()){        //验证添加成功
                return new JsonResult(ResultCode.PARAMS_TYPE_ERROR);
            }
        }
        // 封装返回小程序
        Map<String, String> result = new HashMap<>();
        result.put("session_key", session_key);
        result.put("open_id", open_id);
        result.put("userid",userService.queryByOpenId(open_id).toString());
        return new JsonResult(ResultCode.SUCCESS, result);

    }


}